<template>
    <div :class="[styles, 'upload-photo']" id="uploadPhoto">
        <input type="file" ref="fileInput" class="hidden" @change="uploadFile">
        <div class="new" v-if="isNew" @click="fileInputClick" @mouseleave=" oldPhotoUrl != '' ? isNew = false : isNew = true">
            <i class="fa fa-camera"></i>
            <h5>上传头像</h5>
        </div>
        <img :src="photoUrl" v-if="!isNew" @mouseenter="isNew = true">
    </div>
</template>

<script>
import Query from '../query.js'
import Util from '../util.js'
import SuccessDialog from 'success_alert'
import ErrorDialog from 'error_alert'

export default {
    name: 'uploadPhoto',

    props: {
        oldPhotoUrl: {
            required: true,
            type: String
        },
        styles: {
            required: true,
            type: String,
        },
        postUrl: {
            required: true,
            type: String
        }
    },

    data: () => ({
        photo: '',
        photoUrl: '',
        isNew: true
    }),

    mounted: function mounted() {
        this.isNew = this.oldPhotoUrl === '' ? true : false

        if (!this.isNew) {
            this.photoUrl = this.oldPhotoUrl
        }
    },

    methods: {
        fileInputClick: function () {
            var fileInput = this.$refs.fileInput
            fileInput.click()
        },

        uploadFile: function (e) {
          let self = this
          Util.uploadPhoto(e, this.postUrl+0, function(data) {
            self.photoUrl = data
          })
        }
    }
}
</script>

<style lang="less">
.upload-photo {
    position: relative;

    img{
        width: 100%;
        height: 109px;
    }

    .new {
        width: 100%;
        height: 109px;
        border: 2px dashed #dde0e0;
        display: flex;
        flex-flow: column nowrap;
        justify-content: center;
        align-items: center;
        cursor: pointer;

        i {
            font-size: 20px;
            padding: 10px;
            background-color: #DDE0E0;
            color: #fff;
            border-radius: 50%;
        }
        h5 {
            font-size: 14px;
            color: #DDE0E0;
        }

        &:hover {
            i,
            h5 {
                color: #37CA73;
            }
        }
    }
}
</style>
